#!coding=utf-8
from standard_function import *
import random


def del_appdown_task(driver):
    try:
        btn=WaitforElementById(driver,'com.yingyonghui.market:id/image_mainFragment_manageDownloadEntryIcon',30)
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        btn.click()
    try:
        WaitforElementById(driver,'com.yingyonghui.market:id/text_downloadTitleItem_clear',30).click()
        WaitforElementById(driver,'com.yingyonghui.market:id/button_dialog_confirm',30).click()
    except NoSuchElementException:
        pass
    driver.back()

def clickappdown(driver):
    try:
        c_driver=WaitforElementById(driver,'com.yingyonghui.market:id/recycler_recyclerFragment_content',30)
        btns=WaitforElementsByClass(c_driver,'android.widget.LinearLayout',30)
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        for btn in btns:
            try:
                size=WaitforElementById(btn,"com.yingyonghui.market:id/text_commonAppItem_size",30).get_attribute("text")
            except NoSuchElementException:
                continue
            print size
            if 'G' not in size:
                if 'K' in size or float(size[0:-3])<10:
                    continue
            btn.click()		
            try:
                down_btn=WaitforElementById(driver,'com.yingyonghui.market:id/button_appDetail_download',10)
            except NoSuchElementException:
                driver.back()
                continue
            else:
                down_btn.click()
                try:
                    confirm=WaitforElementById(driver,'com.yingyonghui.market:id/button_dialog_cancel',5)
                    #print "------------------------"
                except NoSuchElementException:
                    pass
                else:
                    confirm.click()
                    driver.back()
                    continue	
                waittime=20
                flag=False
                while waittime:
                    text=''
                    try:
                        text=WaitforElementByClass(down_btn,'android.widget.TextView',5).get_attribute("text")
                    except NoSuchElementException:
                        break
                    else:
                        print text
                        if '0.00' not in text and '0 B/S'not in text:
                            down_btn.click()
                            flag=True
                        else:
                            time.sleep(0.5)
                            if waittime==0:
                                down_btn.click()
                                print "timeout break"
                                break
                            if flag==False:
                                waittime=waittime-1
            print "_________________________"
            driver.back()










def soft(driver):
    try:
        WaitforElementByName(driver,u'软件',30).click()
        WaitforElementById(driver,'com.yingyonghui.market:id/text_normalSkinLinkItem_name3',30).click()
    except NoSuchElementException:
        raise NoSuchElementException
    categorys = [u'视频·音乐',u'购物支付',u'系统安全',u'手机管理',u'上网下载',u'学习办公',u'社交通讯',u'拍摄修图', \
                u'吃喝玩乐',u'实用工具',u'主题铃声',u'新闻资讯',u'读书读图',u'生活健康',u'出行必备',u'金融理财',u'名门应用',u'趣味搞怪',u'运动健康',u'艺术收藏',u'母婴专区',u'游戏工具']
#    categorys = [u'名门应用',u'趣味搞怪',u'运动健康',u'艺术收藏',u'母婴专区',u'游戏工具']
    #print random.randint(0,8)
    time.sleep(2)
    swipeUp(driver,0.9,random.randint(0,8)*0.1)
    for i in categorys:
        print "Current categorys is:",i
        try:
            WaitforElementByName(driver,i,5).click()
        except NoSuchElementException:
            continue

        try:
            WaitforElementByName(driver,u'全部',30).click()
        except NoSuchElementException:
            driver.back()
            continue
        else:
            try:
                clickappdown(driver)
            except NoSuchElementException:
                raise NoSuchElementException
            else:
                driver.back()
        #driver.back()

def game(driver):
    categorys = [u'网络游戏',u'休闲游戏',u'竞速驾驶',u'棋牌桌游',u'动作冒险',u'射击游戏',u'角色扮演',u'经典小品', \
                u'虚拟养成',u'战争策略',u'塔防游戏',u'经营建设',u'体育运动',u'飞行游戏',u'解谜脱逃',u'智力游戏',u'音乐游戏']
    #categorys = [u'动作冒险']
    try:
        WaitforElementByName(driver,u'游戏',30).click()
        WaitforElementById(driver,'com.yingyonghui.market:id/text_normalSkinLinkItem_name3',30).click()
    except NoSuchElementException:
        raise NoSuchElementException
    print random.randint(0,8)
    time.sleep(2)
    swipeUp(driver,0.9,random.randint(0,8)*0.1)
    for i in categorys:
        print "Current categorys is:",i
        try:
            WaitforElementByName(driver,i,5).click()
        except NoSuchElementException:
            continue

        try:
            WaitforElementByName(driver,u'全部',30).click()
        except NoSuchElementException:
            driver.back()
            continue
        else:
            try:
                clickappdown(driver)
            except NoSuchElementException:
                raise NoSuchElementException
            else:
                driver.back()
        #driver.back()



def run():
    desired_caps = {}
    #desired_caps['automationName'] = 'uiautomator2'
    desired_caps['platformName'] = "Android"               # 声明是ios还是Android系统
    desired_caps['platformVersion'] = ANDROID_VERSION            # Android内核版本号，也可以在模拟器设置中查看
    desired_caps['deviceName'] = DEVICE         # 连接的设备名称，可通过adb devices查看
    desired_caps['appPackage'] = 'com.yingyonghui.market'    # apk的包名
    desired_caps['appActivity'] = '.activity.SplashActivity'          # apk的launcherActivity
    desired_caps['noReset']='true'                         #不需要再次安装apk  
    driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)          # 建立 session
    #driver.implicitly_wait(30)
    time.sleep(2)
    del_appdown_task(driver)
    soft(driver)
    driver.back()
    game(driver)
    driver.quit()

#run()